﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoiffeurProject.DBMS.Entities;

namespace CoiffeurProject.DBMS.Tables
{
    public class Notifications
    {
        #region Fields

        Database db;
        List<Birthday> customers_birthday;
        List<Birthday> employees_birthday;

        #endregion

        #region Constructors

        public Notifications(Database db)
        {
            this.db = db;

            GenerateCustomersNotification();
            GenerateEmployeesNotification();
        }

        #endregion

        #region Private Methods

        private void GenerateEmployeesNotification()
        {
            employees_birthday = new List<Birthday>();

            for (int i = 0; i < db.Employees.List.Count; i++)
            {
                var empl = db.Employees.List[i];

                if (empl.NextToBirthday())
                    employees_birthday.Add(new Birthday()
                    {
                        LinkedPerson = empl,
                        Description = BirthdayNotificationPrefix(empl.Birthday) + empl.FullName + " Compleanno"
                    });
            }

            employees_birthday = employees_birthday.OrderBy(b => b.LinkedPerson.Birthday.Day).ToList();
        }

        private void GenerateCustomersNotification()
        {
            customers_birthday = new List<Birthday>();

            for (int i = 0; i < db.Customers.List.Count; i++)
            {
                var cust = db.Customers.List[i];

                if (cust.NextToBirthday())
                    customers_birthday.Add(new Birthday() { LinkedPerson = cust,
                    Description = BirthdayNotificationPrefix(cust.Birthday) + cust.FullName + " Compleanno"});
            }

            customers_birthday = customers_birthday.OrderBy(b => b.LinkedPerson.Birthday.Day).ToList();
        }

        private String BirthdayNotificationPrefix(DateTime birthday)
        {
            var days_to_birthday = birthday.Day - DateTime.Now.Day;

            switch (days_to_birthday)
            { 
                case 0:
                    return "Oggi: ";
                case 1:
                    return "Domani: ";
                default:
                    return "Dopodomani: ";
            }
        }

        #endregion

        #region Properties

        public List<Birthday> CustomersNotifications
        {
            get { return customers_birthday; }
        }

        public List<Birthday> EmployeesNotifications
        {
            get { return employees_birthday; }
        }

        #endregion
    }

    #region Support Structs

    public struct Birthday
    {
        public Person LinkedPerson;
        public String Description;
    }

    #endregion
}
